 '**************************************************************
'CATECO_UDF_001.0  catia / ecotect interoperability
'ben howes, matt naugle, alex gonzales - 05.09.11
'
'This work is licensed under the GNU Generic Public License v3.
'See http://www.gnu.org/licenses/gpl.html For more license information.
'
'
'Creates a link between catia And ecotect Using catia's exposed COM objects 
'And ecotect's dde server.  Props to the proving ground (and uto) for the dde trick.
'
'
'**************************************************************

Public Partial Class MainForm
	
	''///DECLARE PUBLIC VARIABLES///''
	''Dim myCatecoMain As CatecoMain
	
	Private myInsolationName As String
	Private myInsolation As String
	Private myInsolationInt As Integer
	Private myAccumulation As String
	Private myAccumulationInt As Integer
	Private myCalcObject As String
	Private myCalcObjectInt As Integer
	Private myWeatherFileLocation As String
	Private myShadingFileLocation As String
	Private myStartDate As Integer
	Private myEndDate As Integer
	Private myStartTime As Decimal
	Private myEndTime As Decimal
	Private myUParam As String
	Private myVParam As String
	Private mySurfSet As String
	Private myCurvesSet As String
	
	''**Publish private variables
	ReadOnly Property readMyUParam() As String
		Get
			Return myUParam
		End Get
	End Property
	
	ReadOnly Property readMyVParam() As String
		Get
			Return myVParam
		End Get
	End Property
	
	ReadOnly Property readMySurfSet() As String
		Get
			Return mySurfSet
		End Get
	End Property
	
	ReadOnly Property readMyCurvesSet() As String
		Get
			Return myCurvesSet
		End Get
	End Property

	ReadOnly Property readMyInsolationName() As String
		Get
			Return myInsolationName
		End Get
	End Property
	
	ReadOnly Property readMyInsolationInt() As Integer
		Get 
			Return myInsolationInt
		End Get
	End Property
	
	ReadOnly Property readMyAccumulationInt() As Integer
		Get
			Return myAccumulationInt
		End Get
	End Property
	
	ReadOnly Property readMyWeatherFileLocation() As String
		Get
			Return myWeatherFileLocation
		End Get
	End Property
	
	ReadOnly Property readMyShadingFileLocation() As String
		Get
			Return myShadingFileLocation
		End Get
	End Property
	
	ReadOnly Property readMyStartDate() As String
		Get
			Return myStartDate
		End Get
	End Property
	
	ReadOnly Property readMyEndDate() As String
		Get
			Return myEndDate
		End Get
	End Property
	
	ReadOnly Property readMyStartTime() As Decimal
		Get
			Return myStartTime
		End Get
	End Property
	
	ReadOnly Property readMyEndTime() As Decimal
		Get
			Return myEndTime
		End Get
	End Property
	
	ReadOnly Property readMyCalcObjectInt() As Integer
		Get
			Return myCalcObjectInt
		End Get
	End Property
	
	''///POPULATE FORM FOR UI CHOICES///''
	Public Sub New()
		
		initCatia
		getCatiaSetsAndParams
		
		''**The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
			
		
		''**populate cboCurvesOGS [CURVE ordered geometrical set from Catia]
		With Me.cboCurvesOGS
			
			Dim myOrderStrings() As String
			ReDim myOrderStrings(0 To readOrderedSetsList.Length-1)
			For i =0 To readOrderedSetsList.Length-2
				myOrderStrings(i) = readOrderedSetsList(i)
				.Items.Add(myOrderStrings(i))
			Next i
		End With
		
		''**populate cboSurfaceOGS [SURF ordered geometrical set from Catia]
		With Me.cboSurfaceOGS
			Dim myOrderStrings() As String
			ReDim myOrderStrings(0 To readOrderedSetsList.Length-1)
			For i =0 To readOrderedSetsList.Length-2
				myOrderStrings(i) = readOrderedSetsList(i)
				.Items.Add(myOrderStrings(i))
			Next i
		End With
		
		''**populate cboUparam [curvenumber paramter from Catia]
		With Me.cboUParam
			Dim myParamStrings() As String
			ReDim myParamStrings(0 To readParameterList.Length-1)
			For i =0 To readParameterList.Length-2
				myParamStrings(i) = readParameterList(i)
				.Items.Add(myParamStrings(i))
			Next i
		End With
		
		''**populate cboVparam [curvenumber paramter from Catia]
		With Me.cboVParam
			Dim myParamStrings() As String
			ReDim myParamStrings(0 To readParameterList.Length-1)
			For i =0 To readParameterList.Length-2
				myParamStrings(i) = readParameterList(i)
				.Items.Add(myParamStrings(i))
			Next i
		End With
	
		''**populate cboInsolation [Insolation Calculations]
		With Me.cboInsolation
    		.Items.add ("Incidence") 			'index 0
        	''.Items.add ("Absorption")			'index 1
        	''.Items.add ("Skyfactor")			'index 2
        	''.Items.add ("Shading")			'index 3
        	''.Items.add ("Reference")			'index 4
        	''.Items.add ("Comparison")			'index 5
		End With
		Me.cboInsolation.Text = "Incidence"
		
		''**populate cboAccumulation [Insolation Accumulations]
		With Me.cboAccumulation
			.Items.Add ("Cumulative")			'index 0
			.Items.Add ("Average Daily")		'index 1
			.Items.Add ("Average Hourly")		'index 2
			.Items.Add ("Peak")					'index 3
		End With
		Me.cboAccumulation.Text = "Cumulative"
		
		''**populate cboCalcObject [Select3D bool]
		With Me.cboCalcObject
			.Items.Add ("All Visible Objects")	'index 0
			''.Items.Add ("Only Selected Objects")'index 1
		End With
		Me.cboCalcObject.Text = "All Visible Objects"
		
		''**populate start & end time boxes
		Me.numericUpDownStartTime.Value = 7
		Me.numericUpDownEndTime.Value = 20
	End Sub
	
	''///ADDS HELP FOR GUI///''
	Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
   ''**Create the ToolTip and associate with the Form container.
   Dim toolTip1 As New ToolTip()

   ''**Set up the delays for the ToolTip.
   toolTip1.AutoPopDelay = 5000
   toolTip1.InitialDelay = 1000
   toolTip1.ReshowDelay = 500
   ''**Force the ToolTip text to be displayed whether or not the form is active.
   toolTip1.ShowAlways = True

   ''**Set up the ToolTip text for the Button and Checkbox.
   toolTip1.SetToolTip(Me.cmdBrowse, "Click to load weather file for project location")
   toolTip1.SetToolTip(Me.cmdBrowseShading, "Click to load an ecotect file containing shading geometry")
   toolTip1.SetToolTip(Me.cmdRun, "Click to run analysis")
   toolTip1.SetToolTip(Me.numericUpDownStartDate, "Dates are based on the Julian Calendar.  Jan 1st = 1 to Dec 31st = 365")
   toolTip1.SetToolTip(Me.numericUpDownEndDate, "Dates are based on the Julian Calendar.  Jan 1st = 1 to Dec 31st = 365")
   toolTip1.SetToolTip(Me.textboxInput, "Type the exact, case-sensitive, name of the Catia parameter in your UDF where the insolation value will be written to.")
   toolTip1.SetToolTip(Me.cboCurvesOGS, "Select the name of the Ordered Geometrical Set in your Catia part that is named CURVES.")
   toolTip1.SetToolTip(Me.cboSurfaceOGS, "Select the name of the Ordered Geometrical Set in your Catia part that is named SURF.")
   toolTip1.SetToolTip(Me.cboUParam, "This should be your Catia curvenumber parameter.")
   toolTip1.SetToolTip(Me.cboVParam, "This should be your Catia surfnumber parameter.")
End Sub
	
	
	''///BROWSE TO WEATHER FILE///'' 
	Sub browseFile()
		Dim fdlg As OpenFileDialog = New OpenFileDialog()
		With fdlg
			.Title = "Browse to weather file"
			.InitialDirectory = "c:\"
			.Filter = ".wea files (*.wea*)|*.wea*"
			.FilterIndex = 2
			.RestoreDirectory = True
			If .ShowDialog() = DialogResult.OK Then
				Me.textBoxWeatherFile.Text = fdlg.FileName
			End If	
		End with
	End Sub
	
	''///BROWSE TO ECOTECT SHADING GEO FILE///''
	Sub browseShadingFile()
		Dim fdlg As OpenFileDialog = New OpenFileDialog()
		With fdlg
			.Title = "Browse to 3D geometry Ecotect file for shading objects"
			.InitialDirectory = "c:\"
			.Filter = ".eco files (*.eco*)|*.eco*"
			.FilterIndex = 2
			.RestoreDirectory = True
			If .ShowDialog() = DialogResult.OK Then
				Me.textBoxShadingFile.Text = fdlg.FileName
			End If	
		End with
	End Sub	
	
	''///IF DATE RANGE IS CHECKED THEN..///''
	Sub CheckBoxRangeCheckedChanged(sender As Object, e As EventArgs)
		If checkBoxRange.Checked = True Then
			radioButtonMarch.Enabled = False
			radioButtonJune.Enabled = False
			radioButtonDec.Enabled = False
			radioButtonSept.Enabled = False
			checkBoxDesignDay.Enabled = False
		Else
			radioButtonMarch.Enabled = True
			radioButtonJune.Enabled = True
			radioButtonDec.Enabled = True
			radioButtonSept.Enabled = True
			checkBoxDesignDay.Enabled = True
		End If
	End Sub
	
	''///IF DESIGN DAY IS CHECKED THEN..///''
	Sub CheckBoxDesignDayCheckedChanged(sender As Object, e As EventArgs)
		If checkBoxDesignDay.Checked = True Then
			numericUpDownStartDate.Enabled = False
			numericUpDownEndDate.Enabled = False
			checkBoxRange.Enabled = False
		Else
			numericUpDownStartDate.Enabled = True
			numericUpDownEndDate.Enabled = True
			checkBoxRange.Enabled = True
		End If
	End Sub
	
	''///CLOSE THE FORM ON BUTTON CLOSE CLICK///''
	Sub CmdCloseClick(sender As Object, e As EventArgs)
		Me.Close
	End Sub
	
	''///RUN CATECO ON BUTTON RUN CLICK///''
	Sub CmdRunClick(sender As Object, e As EventArgs)
		getMyInputs
	End Sub
	
	''///BROWSE TO WEATHER FILE ON ... BUTTON CLICK///''
	Sub CmdBrowseClick(sender As Object, e As EventArgs)
		browseFile
	End Sub
	
	''///BROWSE TO ECOTECT 3D GEO SHADING FILE ON ... BUTTON CLICK///''
	Sub CmdBrowseShadingClick(sender As Object, e As EventArgs)
		browseShadingFile
	End Sub
	

	''///GETS ALL INTS AND STRINGS FOR USE IN CATECO THEN RUNS CatecoTester///''
	Public Sub getMyInputs()
		''**variables
		myInsolationName = textboxInput.Text
		myInsolation = cboInsolation.Text
		myInsolationInt = cboInsolation.SelectedIndex
		myAccumulation = cboAccumulation.Text
		myAccumulationInt = cboAccumulation.SelectedIndex
		myCalcObject = cboCalcObject.Text
		myCalcObjectInt = cboCalcObject.SelectedIndex
		myWeatherFileLocation = Me.textBoxWeatherFile.Text
		myShadingFileLocation = Me.textBoxShadingFile.Text
		Dim myDay As Integer
		myStartTime = numericUpDownStartTime.Value
		myEndTime = numericUpDownEndTime.Value
		myUParam = cboUParam.Text
		myVParam = cboVParam.Text
		mySurfSet = cboSurfaceOGS.Text
		myCurvesSet = cboCurvesOGS.Text 
		
		''**Get day or range to run analysis on
		If checkBoxRange.Checked = False And checkBoxDesignDay.Checked = False Then
			myDay = 1000 ''throws an error
		End If 
		If checkBoxDesignDay.Checked = True Then
			If radioButtonJune.Checked = True Then
				myStartDate = 176
				myEndDate = 176
			End if
			If radioButtonSept.Checked = True Then
				myStartDate = 266
				myEndDate = 266
			End if
			If radioButtonDec.Checked = True Then
				myStartDate = 356
				myEndDate = 356
			End if
			If radioButtonMarch.Checked = True Then
				myStartDate = 80
				myEndDate = 80
			End If
		End if
		If checkBoxRange.Checked = True 
			myStartDate = Me.numericUpDownStartDate.Value
			myEndDate = Me.numericUpDownEndDate.Value
		End if
			
		''**Error checking***************************
		''**Check for a parameter name to pass to catia
		If myInsolationName = "" Then
			MsgBox ("Error: no parameter name specified")
		''**Check to make sure analysis days are valid
		ElseIf checkBoxDesignDay.Checked = True And radioButtonJune.Checked = False And radioButtonSept.Checked = False And radioButtonDec.Checked = False And radioButtonMarch.Checked = False Then
			MsgBox ("Error: select a design day")
		ElseIf myDay > 365 Then
			MsgBox ("Error: select one analysis day type")
		''**Check to make sure start time is before end time
		ElseIf myStartTime > myEndTime Then
			MsgBox ("Error: start time is after end time")
		Else
			''MsgBox (myShadingFileLocation & "_" & myInsolationName & "_" & myInsolation & myInsolationInt & "_" & myAccumulation & myAccumulationInt & "_" & myCalcObject & myCalcObjectInt & "_" & myWeatherFileLocation & "_" & "start" & "_" & myStartDate & "end" & "_" & myEndDate )
			myMain
		End If
	End Sub
	
	
	
	
End Class
